import axios from 'axios'
import {
  get,
  post
} from '@/utils/request'

export const browserDebug = false
const prefix = browserDebug ? '/api' : ''

/**
 * 接口授权
 * @param {Object} data 授权参数信息
 */
export const authorize = data => post('/authorize', data, true)

/**
 * 账号密码登录接口
 * @param {*} data
 */
export const wechatAuthorize = function (code) {
  return axios.post(`${prefix}/authorize/wechat`, {
    code: code,
    platform: 1
  })
}

/**
 * 获取首页
 * @param {Object} params [请求的url地址]
 */
export const getHomepage = params => get(`${prefix}/home`, params)

/**
 * 获取商品详情
 * @param {*} id 商品ID
 */
export const getSpu = id => get(`${prefix}/shop/spu/${id}`)

/**
 * 添加商品进购物车
 * @param {*} data 商品信息
 */
export const pushToCart = function (data) {
  return post(`${prefix}/shop/cart/add`, data)
}

/**
 * 获取商品 sku 详情
 * @param {Object} params [请求的url地址]
 */
export const getIssuedApp = params => get(`${prefix}/app/issue/view`, params)

/**
 * 获取购物车数据
 */
export const cartApi = function () {
  return axios.get(`${prefix}/shop/cart`)
}

/**
 * 账号密码登录接口
 * @param {*} data
 */
export const login = function (data) {
  if (Object.prototype.hasOwnProperty.call(data, 'captcha')) {
    return post(`${prefix}/passport/login/sms`, data)
  } else if (Object.prototype.hasOwnProperty.call(data, 'password')) {
    return post(`${prefix}/passport/login`, data)
  } else {
    console.log('Error login mode!')
  }
}

/**
 * 发送短信验证码接口
 * @param {*} data
 */
export const sendSmsCode = function (data) {
  return axios.post(`${prefix}/sms/captcha`, data)
}

/**
 * 获取签到列表接口
 */
export const signinListApi = function () {
  return axios.get(`${prefix}/user/signin`)
}

/**
 * 今日签到接口
 */
export const signinCreateApi = function () {
  return axios.post(`${prefix}/user/signin/create`)
}

/**
 * export const getProductInfo = (id) => axios({
  method: 'get',
  url: '/shop/spu/' + id,
  params: {},
  headers: {}
})
*/

/**
 * 获取商品详情
 * @param id 商品ID
 */
export const getProductInfoApi = (id) => {
  return axios.get('/shop/spu/' + id)
}

/**
 * 获取当前购物车商品数量
 * @param none
 */
export const getCartNumberApi = () => axios({
  method: 'get',
  url: '/shop/cart/number',
  headers: {
    FastMode: true
  }
})

/**
 * 加入购物车
 * @param data 参数
 * ```
 * {
 *   product_id: 商品ID,
 *   goods_id: SKU ID,
 *   nums: 数量
 * }
 * ```
 */
export const postAddCartApi = (data) => axios({
  method: 'post',
  url: '/shop/cart/add',
  data: data,
  headers: {
    FastMode: true
  }
})

/**
 * 商城收藏商品、店铺
 * @param data 参数信息
 * ```
 * {
 *   product_id: 商品ID,
 *   store_id: 店铺ID
 * }
 * ```
 */
export const postShopCollectApi = (data) => axios({
  method: 'post',
  url: '/shop/extend/collect',
  data: data,
  headers: {
    FastMode: true
  }
})
